import numpy as np

class CIC:
    def __init__(self,order,dic):
        self.order = order
        self.dic = dic
        self.sum = np.zeros(order,dtype=complex)
        self.dif = np.zeros(order,dtype=complex)
        self.c = 0

    def Done(self,din):
        sc = self.c
        ps = 0
        dl = self.c + len(din)
        os = dl // self.dic
        self.c %= self.dic
        normal = 1.0
        for i in range(self.order):
            normal /= self.dic
        dout = np.zeros(os,dtype=complex)
        for v in din[:]:
            sx = v
            for s in range(self.order):
                sx += self.sum[s]
                self.sum[s] = sx
            sc += 1
            if sc == self.dic:
                for s in range(self.order):
                    sx, self.dif[s] = sx-self.dif[s], sx
                dout[ps] = sx * normal
                sc = 0
                ps += 1
        return dout